﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KWIS9000SERVER
{
    public partial class Form1 : Form
    {
        private ListBox clientsList;
        private GroupBox groupBox1;
        private RadioButton radioBlokken;
        private RadioButton radioZaalquiz;
        private Button button2;
        private GroupBox groubBoxConnections;
        private Button buttonConnectionsNext;
        KwisServer kwisServer;
    
        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.clientsList = new System.Windows.Forms.ListBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button2 = new System.Windows.Forms.Button();
            this.radioZaalquiz = new System.Windows.Forms.RadioButton();
            this.radioBlokken = new System.Windows.Forms.RadioButton();
            this.groubBoxConnections = new System.Windows.Forms.GroupBox();
            this.buttonConnectionsNext = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groubBoxConnections.SuspendLayout();
            this.SuspendLayout();
            // 
            // clientsList
            // 
            this.clientsList.FormattingEnabled = true;
            this.clientsList.Location = new System.Drawing.Point(6, 19);
            this.clientsList.Name = "clientsList";
            this.clientsList.Size = new System.Drawing.Size(406, 95);
            this.clientsList.TabIndex = 0;
            this.clientsList.SelectedIndexChanged += new System.EventHandler(this.clientsList_SelectedIndexChanged);
            this.clientsList.SizeChanged += new System.EventHandler(this.clientsList_SizeChanged);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioBlokken);
            this.groupBox1.Controls.Add(this.radioZaalquiz);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Location = new System.Drawing.Point(514, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(418, 254);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Quiz type selectie";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(310, 201);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(102, 47);
            this.button2.TabIndex = 1;
            this.button2.Text = "Volgende";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // radioZaalquiz
            // 
            this.radioZaalquiz.AutoSize = true;
            this.radioZaalquiz.Location = new System.Drawing.Point(19, 100);
            this.radioZaalquiz.Name = "radioZaalquiz";
            this.radioZaalquiz.Size = new System.Drawing.Size(65, 17);
            this.radioZaalquiz.TabIndex = 2;
            this.radioZaalquiz.TabStop = true;
            this.radioZaalquiz.Text = "Zaalquiz";
            this.radioZaalquiz.UseVisualStyleBackColor = true;
            // 
            // radioBlokken
            // 
            this.radioBlokken.AutoSize = true;
            this.radioBlokken.Location = new System.Drawing.Point(19, 123);
            this.radioBlokken.Name = "radioBlokken";
            this.radioBlokken.Size = new System.Drawing.Size(64, 17);
            this.radioBlokken.TabIndex = 3;
            this.radioBlokken.TabStop = true;
            this.radioBlokken.Text = "Blokken";
            this.radioBlokken.UseVisualStyleBackColor = true;
            // 
            // groubBoxConnections
            // 
            this.groubBoxConnections.Controls.Add(this.buttonConnectionsNext);
            this.groubBoxConnections.Controls.Add(this.clientsList);
            this.groubBoxConnections.Location = new System.Drawing.Point(21, 12);
            this.groubBoxConnections.Name = "groubBoxConnections";
            this.groubBoxConnections.Size = new System.Drawing.Size(418, 254);
            this.groubBoxConnections.TabIndex = 4;
            this.groubBoxConnections.TabStop = false;
            this.groubBoxConnections.Text = "Verbindingen";
            // 
            // buttonConnectionsNext
            // 
            this.buttonConnectionsNext.Enabled = false;
            this.buttonConnectionsNext.Location = new System.Drawing.Point(310, 201);
            this.buttonConnectionsNext.Name = "buttonConnectionsNext";
            this.buttonConnectionsNext.Size = new System.Drawing.Size(102, 47);
            this.buttonConnectionsNext.TabIndex = 2;
            this.buttonConnectionsNext.Text = "Volgende";
            this.buttonConnectionsNext.UseVisualStyleBackColor = true;
            this.buttonConnectionsNext.Click += new System.EventHandler(this.buttonConnectionsNext_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(944, 586);
            this.Controls.Add(this.groubBoxConnections);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Server";
            this.Load += new System.EventHandler(this.Form1_Load_1);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groubBoxConnections.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            kwisServer = new KwisServer();

            kwisServer.host.server.onConnection += new NetComm.Host.onConnectionEventHandler(Server_onConnection);
            kwisServer.host.server.lostConnection += new NetComm.Host.lostConnectionEventHandler(Server_lostConnection);

            
        }

        // connection events 
        void Server_onConnection(string id)
        {
            clientsList.DataSource = kwisServer.host.server.Users;
            clientsList.Update();
        }

        void Server_lostConnection(string id)
        {
            clientsList.DataSource = kwisServer.host.server.Users;
            clientsList.Update();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (radioZaalquiz.Checked ||radioBlokken.Checked)
            {
                if (radioZaalquiz.Checked)
                {
                    // we starten een zaalquiz en wachten op namen van spelers
                }
                else
                {
                    // we starten een blokken spel en wachten op namen van spelers
                }
            }
        }

        private void clientsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonConnectionsNext.Enabled = true;
        }

        private void clientsList_SizeChanged(object sender, EventArgs e)
        {

        }

        private void buttonConnectionsNext_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < kwisServer.host.server.Users.Count; i++)
            {
                kwisServer.host.server.SendData(kwisServer.host.server.Users[i], ConvertStringToBytes("startQuiz|") );
            }
        }

        // helper functions
        string ConvertBytesToString(byte[] bytes)
        {
            return ASCIIEncoding.ASCII.GetString(bytes);
        }

        byte[] ConvertStringToBytes(string str)
        {
            return ASCIIEncoding.ASCII.GetBytes(str);
        }

    }
}
